//
//  DJSettingViewController.m
//  DualJoy
//
//  Created by lijunge on 2019/11/14.
//  Copyright © 2019 lijunge. All rights reserved.
//

#import "DJSettingViewController.h"
#import "DJSwitchTableViewCell.h"
#import "DJDetailTableViewCell.h"
#import <Masonry.h>
#import "DJConstant.h"
#import "DJLoginViewController.h"
#import <GoogleSignIn/GoogleSignIn.h>
#import "ProgressHUD.h"
@interface DJSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation DJSettingViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor colorWithRed:232.0 / 255.0 green:233.0 / 255.0 blue:239.0 / 255.0 alpha:1.0];
    [self configSubViews];
    [self readUserInfoFromLocal];
}

- (void)configSubViews {
  
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    CGFloat bottomMargin = kDJBottomMargin + 20;
   
//    UIImageView *backImageView = [[UIImageView alloc] init];
//    backImageView.image = [UIImage imageNamed:@"launchImage"];
//    backImageView.contentMode = UIViewContentModeScaleAspectFit;
//    [self.view addSubview:backImageView];
    
    self.headerView = [[UIView alloc] init];
    self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 120 + kDJNavBarHeight);
    self.headerView.backgroundColor = [UIColor colorWithRed:39 / 255.0 green:49.0 / 255.0 blue:57.0 / 255.0 alpha:1.0];
    //
    //kVCBackColor;
    [self.headerView addSubview:self.headImageView];
    [self.headerView addSubview:self.nameLabel];
    
    [self.view addSubview:self.headerView];
    
    UIButton *closeButton = [[UIButton alloc] init];
    [closeButton setImage:[UIImage imageNamed:@"set_close"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(closeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:closeButton];
    
    self.footerView = [[UIView alloc] init];
    self.footerView.backgroundColor = [UIColor clearColor];
    //[UIColor colorWithRed:232.0 / 255.0 green:233.0 / 255.0 blue:239.0 / 255.0 alpha:1.0];
    [self.view addSubview:self.footerView];
    
    UIButton *footerButton = [[UIButton alloc] init];
    footerButton.backgroundColor = [UIColor colorWithRed:39 / 255.0 green:49.0 / 255.0 blue:57.0 / 255.0 alpha:1.0];//kVCBackColor;
    [footerButton setTitle:@"LOG OUT" forState:UIControlStateNormal];
    [footerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    footerButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    footerButton.layer.cornerRadius = 5.0;
    [footerButton addTarget:self action:@selector(footerButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:footerButton];
    
    //[self.view addSubview:self.tableView];
   // self.tableView.tableHeaderView = self.headerView;
    
//    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.mas_equalTo(self.view);
//        make.height.mas_equalTo(120 + kDJNavBarHeight);
//    }];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.height.mas_equalTo(120 + kDJNavBarHeight);
    }];
    
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(60);
        make.bottom.equalTo(self.view.mas_bottom).offset(-bottomMargin);
    }];
    
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.equalTo(self.view);
//        make.bottom.equalTo(self.footerView.mas_top);
//    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_left).offset(20);
        make.width.height.mas_equalTo(80);
        make.top.equalTo(self.headerView).offset(kDJNavBarHeight + 20);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(20);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(self.headImageView);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(44);
        make.right.equalTo(self.headerView.mas_right).offset(-20);
        make.top.equalTo(self.headerView).offset(kDJStatusBarHeight);
    }];
    
    [footerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.footerView).offset(20);
        make.right.equalTo(self.footerView.mas_right).offset(-20);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(self.footerView);
    }];
}

#pragma mark - Private

- (void)readUserInfoFromLocal {
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] valueForKey:@"user_info"];
    if ([DJTool validObj:dic]) {
        NSString *userNameString = [dic valueForKey:@"user_name"];
        if ([DJTool validStr:userNameString]) {
            self.nameLabel.text = userNameString;
        }
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSString *url = [dic valueForKey:@"user_image"];
            if ([DJTool validStr:url]) {
                UIImage *iconImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.headImageView.layer.cornerRadius = 40.0;
                    self.headImageView.clipsToBounds = YES;
                    self.headImageView.image = iconImage;
                });
            }
        });
    } else {
        [self getUserInfoErrorLoginAgain];
    }
}

#pragma mark -

- (void)footerButtonTapped:(UIButton *)sender {
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] valueForKey:@"user_info"];
    if ([DJTool validObj:dic]) {
        NSString *userTypeString = [dic valueForKey:@"user_type"];
        if ([DJTool validStr:userTypeString]) {
            if ([userTypeString isEqualToString:@"1"]) {
                
            } else if ([userTypeString isEqualToString:@"2"]) {
                
            } else if ([userTypeString isEqualToString:@"3"]) {
                [[GIDSignIn sharedInstance] signOut];
            }
            
        }
    }
    if ([DJTool validObj:dic]) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user_info"];
    }
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DualJoyLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    DJLoginViewController *loginVC = [[DJLoginViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.rootViewController = navi;
}

- (void)closeButtonTapped:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:^{
//    }];
}

- (void)getUserInfoErrorLoginAgain {
    
    [ProgressHUD showError:@"请重新登录"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DualJoyLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    DJLoginViewController *loginVC = [[DJLoginViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.rootViewController = navi;
}

#pragma mark - UITableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        DJSwitchTableViewCell *switchCell = [DJSwitchTableViewCell switchTableViewCellWithTableView:tableView];
        return switchCell;
    }
    DJDetailTableViewCell *detailCell = [DJDetailTableViewCell detailTableViewCellWithTableView:tableView];
    return detailCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -

- (UITableView *)tableView {
    
    if (_tableView == nil) {
        UITableView *tmpTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tmpTableView.rowHeight = 50;
        tmpTableView.autoresizesSubviews = NO;
        tmpTableView.delegate = self;
        tmpTableView.dataSource = self;
        tmpTableView.showsVerticalScrollIndicator = NO;
        tmpTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tmpTableView.backgroundColor = [UIColor colorWithRed:232.0 / 255.0 green:233.0 / 255.0 blue:239.0 / 255.0 alpha:1.0];
        UIView *tmpFView = [[UIView alloc] init];
        tmpFView.backgroundColor = [UIColor whiteColor];
        tmpTableView.tableFooterView = tmpFView;
        _tableView = tmpTableView;
    }
    return _tableView;
}

- (UIImageView *)headImageView {
    
    if (_headImageView == nil) {
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.backgroundColor = [UIColor lightGrayColor];
        headImageView.layer.cornerRadius = 40;
        _headImageView = headImageView;
    }
    return _headImageView;
}

- (UILabel *)nameLabel {
    
    if (_nameLabel == nil) {
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = @"1234";
        nameLabel.font = [UIFont boldSystemFontOfSize:18];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.backgroundColor = [UIColor clearColor];
        //[UIColor colorWithRed:39 / 255.0 green:49.0 / 255.0 blue:57.0 / 255.0 alpha:1.0];
        //kVCBackColor;
        _nameLabel = nameLabel;
    }
    return _nameLabel;
}

@end
